
import { _decorator, Component, Node, tween, v3 } from 'cc';
import { Card } from './Card';
import { SoltaireGame } from './SoltaireGame';
import { SoundEffect } from './tool/SoundEffect';
const { ccclass, property } = _decorator;

/**
 * 一列卡牌，当前同一组卡牌
 */
 
@ccclass('CardGroup')
export class CardGroup extends Component {
    @property(Number)
    groupId: Number= 0;//当前组id

    cards: Card[] = [];

    start () {
        // [3]
    }

    AddOneCard(card)
    {
        this.cards.push(card);
    }

    //获取最终节点
    GetLastCard()
    {
        if (this.cards.length>0) {
            return this.cards[this.cards.length-1];
        }

        return null;
    }
    
    //放置
    PlaceCards(card:Card,delayTime=0)
    {
        let parent;
        if (this.cards.length>0) {
            parent= this.cards[this.cards.length-1].node;
        }else
        {
            parent=this.node;
        }

        let pos=v3(0,-22,0);
        pos.add(parent.worldPosition);
        let temp=this;
        tween(card.node).delay(delayTime).to(0.15, {worldPosition:pos}).call(()=>{
            card.node.setParent(parent);
            card.node.setPosition(0,-22,0);
            temp.UpdataData();
            temp.DetectionCardGroupClear();
        }).start()
    }

    //更新数据
    UpdataData()
    {
        for (let i = 0; i < this.cards.length; i++) {
            this.cards[i].ResetParent(this);
        }
    }

    //更新数据
    RemaveTouch()
    {
        for (let i = 0; i < this.cards.length; i++) {
            this.cards[i].RemaveTouchEvent();
        }
    }

    //翻开新卡牌
    TurnOverNewCard()
    {
        if (this.cards.length!=0) {
            //this.cards[this.cards.length-1].TurnOverCard();
        }
    }

    //当前卡牌是否能拖动
    CardIsTouch(card:Card)
    {
        let isMoveID=0;
        for (let i = 0; i < this.cards.length; i++) {
            if (card==this.cards[i]) {
                isMoveID=i;
                break;
            }
        }

        for (let i = isMoveID; i < this.cards.length-1; i++) {
            if (this.cards[i+1].cardNumber!=this.cards[i].cardNumber-1) {
                return false;
            }
        }

        return true;
    }

    //选中效果
    SelectedCard()
    {
        if (this.cards.length>0) {
            this.cards[this.cards.length-1].SetSelected(true);
        }
        for (let i = this.cards.length-1; i >0 ; i--) {
            if (this.cards[i-1].isTouch) {
                if (this.cards[i].cardNumber==this.cards[i-1].cardNumber-1) {
                    this.cards[i-1].SetSelected(true);
                }else
                {
                    this.cards[i-1].SetSelected(false);
                    break;
                } 
            } else {
                this.cards[i-1].SetSelected(false);
                break;
            }
        }
    }

    ClearSelectedCard()
    {
        for (let i = 0; i <this.cards.length ;i++) {
            this.cards[i].SetSelected(false);
        }
    }

    //检查卡组是否能消除
    DetectionCardGroupClear()
    {
        console.log("DetectionCardGroupClear");
        let startColor=0;
        let isClear=false;
        let clearCards=[];
        let startKId=0;
        for (let i = 0; i < this.cards.length; i++) {
            if (this.cards[i].isTouch==false)
            {
                continue;
            }

            if (this.cards[i].cardNumber==13) 
            {
                isClear=true;
                startColor=this.cards[i].cardColor;
                clearCards=[];
                startKId=i;
            }

            if (isClear) 
            {
                clearCards.push(this.cards[i]);
            }
        }

        if (clearCards.length!=13) 
        {
            return ;
        }

        for (let i = 0; i < clearCards.length; i++) {
            if (clearCards[i].cardColor!=startColor) {
                return ;
            }

            if (clearCards[i].cardNumber!=13-i) {
                return ;
            }
        }

        this.cards.splice(startKId,13)
        //SoltaireGame.instance.ClearOneGroupCard(clearCards,this);
        SoundEffect.instance.playOneShotName("card_kubaru");

        //return clearCards;
    }

}


